<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/7 0007
 * Time: 19:23
 */

namespace App\Models\ActivityTrigger;

class ActivityTriggerService
{
    protected $activityTriggerRepository;

    public function __construct(ActivityTriggerRepository $activityTriggerRepository)
    {
        $this->activityTriggerRepository = $activityTriggerRepository;
    }

    public function create($data)
    {
        $activityTrigger = new ActivityTrigger($data);
        return $this->activityTriggerRepository->create($activityTrigger);
    }
    public function getActivityTriggerListByPage($request, $user_id = '')
    {
        $start = (int)$request->query->get('offset', 0);
        $limit = (int)$request->query->get('limit', 10);
        $keyword = $request->query->get('search');
        $sort = $request->query->get('sort', 'weight');
        $order = $request->query->get('order', 'desc');
        $sortArray = [[$sort,$order]];

        $conditions = [
            ['user_id', $user_id],
            ['group_code', session('groupcode')],
        ];

        if (!is_null($keyword)) {
            $conditions[] = ['name','like','%'.$keyword.'%'];
        }
        $list = $this->activityTriggerRepository->findByConditionsPage($conditions, $sortArray, $start, $limit);

        $list = $list->toArray();

        $total = $this->activityTriggerRepository->countByConditions($conditions);

        return ['total'=>$total,'rows'=>$list ];
    }

    public function saveActivityTriggerById($updates, $id)
    {
        return $this->activityTriggerRepository->update($updates, ['id'=>$id]);
    }

    public function getActivityTriggerById($id)
    {
        return $this->activityTriggerRepository->getActivityTriggerById($id);
    }

    public function deleteActivityTriggers($ids)
    {
        return $this->activityTriggerRepository->deleteByIds($ids);
    }
}
